<?php

class SJB_Classifieds_ActivateListing extends SJB_Function
{
	public function execute()
	{
		$errors = null;
		$invoice_sid = SJB_Request::getVar('invoice_sid', null, 'default', 'int');
		$invoice = SJB_InvoiceManager::getObjectBySID($invoice_sid);

		if (!is_null($invoice)) {

			$status = $invoice->getStatus();
			if ($status == SJB_Invoice::INVOICE_STATUS_VERIFIED) {
				$items_info = $invoice->getPropertyValue('items');
				$listings_id = explode(",", $items_info['custom_info'][1]['listings_ids']);
				foreach ($listings_id as $listing_id)
				{
					SJB_ListingManager::activateListingBySID($listing_id);
				}
				$invoice->setStatus(SJB_Invoice::INVOICE_STATUS_PAID);
				SJB_InvoiceManager::saveInvoice($invoice);

			}
			else {
				if ($status == SJB_Invoice::INVOICE_STATUS_PAID)
					$errors['INVOICE_IS_COMPLETED'] = 1;
				else
					$errors['INVOICE_IS_NOT_VERIFIED'] = 1;
			}
		}
		else {
			$errors['INVALID_INVOICE_ID'] = 1;
		}

		$tp = SJB_System::getTemplateProcessor();
		$tp->assign("errors", $errors);
		$tp->display("activate_listing.tpl");
	}
}
